
package com.c0nflict.server;

public class TableSquare {	
	String eol = System.getProperty("line.separator");
	
	private int dice = 0;				// Number of dice in square
	private int ownerType = 0;			// Type of owner (see top for types)
	private Player owner = null;		// Link to owner if player
	private int id = 0;					// Id of square
	private int color = 0;				// Square color -- ZERO IS GRAY
	
	public int getId() { return id; }
	public void setId(int x) { id = x; }
	
	public int getDice() { return dice; }
	public void setDice(int dice) {
		// Adjust owner's total dice by change in value
		if (owner != null)
			owner.addDice(dice - this.dice);
		
		this.dice = dice;
	}
	public void addDice(int dice) { if (owner != null) owner.addDice(dice); this.dice += dice; }
	
	public int getColor() { return color; }

	public int getOwnerType() { return ownerType; }
	
	public Player getOwner() { return owner; }

	public void setOwner(Player player) {
		if (player == null)
			return;
		
		if (owner != null)
			owner.removeArea(this);
		
		owner = player;
		color = player.getColor();
		ownerType = player.getOwnerType();
		player.addArea(this);
	}
	
	public boolean hasOwner() { return (owner == null) ? false : true; }
	
	public boolean isBlocked() { return (ownerType == Player.OT_BLOCKED); }
	public boolean isEmpty() { return (ownerType == Player.OT_EMPTY); }
	public boolean isNobody() { return (ownerType == Player.OT_NOBODY); }
	public boolean isPlayer() { return (ownerType == Player.OT_PLAYER); }
	
	public String toXml() {
		StringBuffer buf = new StringBuffer("  <square id=" + getId() + ">" + eol);

		buf.append("    <owner id=" + owner.getDbId() + " type=" + getOwnerType() + ">" + owner.getName() + "</owner>" + eol);
		buf.append("    <color>" + getColor() + "</color>" + eol);
		buf.append("    <dice>" + getDice() + "</dice>" + eol);
		buf.append("  </square>" + eol);
	 	
	 	return buf.toString();
	}
	
	public TableSquare(Player player, int id) { setOwner(player); this.id = id; }
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {	

	}
}
